using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class ProductItemController : GenericController {

        public ActionResult Index() {
            return View();
        }

        [HttpPost]
        public ActionResult Index(JQGridParm parm) {
            string sidx = parm.Sidx;
            string sord = parm.Sord;
            int page = parm.Page;
            int rows = parm.Rows;

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = unitOfWork.ProductItemRepository.dbSet.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var entities = unitOfWork.ProductItemRepository.Get(pageIndex, pageSize, sidx, sord, parm.Filters);

            var enums = entities.Select(e => new {
                id = e.ProductItemId,
                cell = new object[] { 
                    e.ProductId,
                    e.Product.Number,
                    e.Product.Name,
                    e.Product.Spec,
                    e.Product.Model,
                    e.Product.Unit,
                    e.SupplierId,
                    e.Supplier.Number,
                    e.Supplier.Name,
                    e.Remark
                }
            });

            var jsonData = new {
                success = true,
                total = totalPages,
                page,
                records = totalRecords,
                rows = enums
            };
            return Json2(jsonData);
        }

        [HttpPost]
        public ActionResult Add(ProductItem productitem) {
            ProductItem productitemAdd = new ProductItem();
            if (TryUpdateModel(productitemAdd)) {
                unitOfWork.ProductItemRepository.Add(productitemAdd);
                unitOfWork.Save();
                return Json2(new { success = true });
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Edit(int? id, ProductItem productitem) {
            if (id != null) {
                ProductItem productitemEdit = unitOfWork.ProductItemRepository.Get((int)id);
                if (productitemEdit != null) {
                    if (TryUpdateModel(productitemEdit)) {
                        unitOfWork.Save();
                        return Json2(new { success = true });
                    }
                }
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            if (string.IsNullOrWhiteSpace(id))
                Json2(new { success = false, message = errorMessage });
            try {
                unitOfWork.ProductItemRepository.Remove(id);
                unitOfWork.Save();
                return Json2(new { success = true });
            } catch (ArgumentNullException e) {
                return Json2(new { success = false, message = e.Message });
            } catch (Exception e) {
                return Json2(new { success = false, message = errorMessage });
            }
        }

        public ActionResult ProductItems() {
            var productitems = unitOfWork.ProductRepository.dbSet.Select(s => new { key = s.ProductId, value = s.Number + " " + s.Name }).AsEnumerable();
            var jsonData = new { value = productitems };
            return Json2(jsonData);
        }

        //public ActionResult GetById(string ids) {
        //    var idlist = ids.ToIntList();
        //    List<ProductItem> list = new List<ProductItem>();
        //    foreach (int id in idlist) {
        //        var e = unitOfWork.ProductItemRepository.Get(id);
        //        if (e != null)
        //            list.Add(e);
        //    }

        //    var enums = list.Select(e => new {
        //        id = e.ProductId,
        //        cell = new object[] { 
        //        e.Product.Number,
        //            e.Product.Name,
        //            e.Product.Model,
        //            e.Product.Unit
        //        }
        //    });

        //    var jsonData = new {
        //        success = true,
        //        id = list[0].SupplierId,
        //        supplier = list[0].Supplier.Number + " " + list[0].Supplier.Name,
        //        row = enums
        //    };

        //    return Json2(jsonData);

        //    //return Json2(new { success = false, message = errorMessage });
        //}

        public ActionResult GetById(int id) {
            var productItem = unitOfWork.ProductItemRepository.Get(id);
            if (productItem != null) {
                var supplier = unitOfWork.SupplierRepository.Get(productItem.SupplierId);
                if (supplier != null) {
                    var supplierUsers = supplier.SupplierUsers;

                    var enums = supplierUsers.Select(e => new {
                        key = e.SupplierUserId,
                        value = e.Name
                    });
                    var jsonData = new {
                        success = true,
                        supplier = productItem.Supplier.Number + " " + productItem.Supplier.Name,
                        address = productItem.Supplier.Contacts.Address,
                        phone = productItem.Supplier.Contacts.Phone,
                        fax = productItem.Supplier.Contacts.Fax,
                        value = enums
                    };
                    return Json2(jsonData);
                }
            }
            return Json2(new { success = false, message = errorMessage });

        }

        public ActionResult GetProduct(string term) {
            var jsonData = unitOfWork.ProductRepository.GetPartsSemi(term)
                .Select(p => new { id = p.Id, value = p.Name, label = p.Number + " " + p.Name });
            return Json2(jsonData);
        }

        public ActionResult GetSupplier(string term) {
            var jsonData = unitOfWork.SupplierRepository.Get(term)
                .Select(p => new { id = p.Id, value = p.Name, label = p.Number + " " + p.Name });
            return Json2(jsonData);
        }
    }


}

